'use strict';
/**
 * Created by FreezeSoul on 2015/10/10.
 */
define([
        'angular',
        'app/modules/schema/define'
    ],
    function (angular) {
        angular.module('ad.schema')
            .factory('adSchemaTreeService', ['$http', '$q', '_', 'adRequest', function ($http, $q, _, adRequest) {

                function getPage(current, size, search) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/schema/search';
                    adRequest(url, {
                        criteria: search, pageIndex: current, pageSize: size
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function getSchemas() {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/schema/get';
                    adRequest(url, {}).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function checkSchemaCode(id, code) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/schema/check';
                    adRequest(url, {
                        schemaId: id,
                        schemaCode: code
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function addSchema(item) {
                    var schemaCode = item.schema_code;
                    var schemaName = item.schema_name;
                    var folderId = item.schema_folder_id;
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/schema/add';
                    adRequest(url, {
                        schemaName: schemaName,
                        schemaCode: schemaCode,
                        folderId: folderId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function removeSchemas(items) {
                    var schemaIds = _.map(items, function (item) {
                        return item.schema_id;
                    });
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/schema/delete';
                    adRequest(url, {
                        schemaIds: angular.toJson(schemaIds)
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function updateSchema(item) {
                    var schemaId = item.schema_id;
                    var schemaCode = item.schema_code;
                    var schemaName = item.schema_name;
                    var schemaData = item.schema_data;
                    var schemaFolderId = item.schema_folder_id;

                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/schema/update';
                    adRequest(url, {
                        schemaId: schemaId,
                        schemaCode: schemaCode,
                        schemaName: schemaName,
                        schemaData: schemaData,
                        schemaFolderId: schemaFolderId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function loadSchema(schemaId) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/schema/load';
                    adRequest(url, {
                        schemaId: schemaId
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function loadSchemaByCode(schemaCode) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dataset/schema/load';
                    adRequest(url, {
                        schemaCode: schemaCode
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                return {
                    getPage: getPage,
                    getSchemas: getSchemas,
                    addSchema: addSchema,
                    loadSchema: loadSchema,
                    loadSchemaByCode: loadSchemaByCode,
                    updateSchema: updateSchema,
                    removeSchemas: removeSchemas,
                    checkSchemaCode: checkSchemaCode
                };
            }]);
    });